package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.vo.album.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.qcloud.vod.VodUploadClient;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;

    /**
     * 根据声音ID+声音数量 获取下单付费声音列表
     * /api/album/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}
     * @param trackId
     * @param trackCount
     * @return
     */
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    @GuiguLogin
    public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable Long trackId,
                                                         @PathVariable Integer trackCount){
        Long userId = AuthContextHolder.getUserId();
        List<TrackInfo> list = trackInfoService.findPaidTrackInfoList(trackId,trackCount,userId);
        return Result.ok(list);
    }

    /**
     * 获取用户声音分集购买支付列表
     * /api/album/trackInfo/findUserTrackPaidList/{trackId}
     * @return
     */
    @GuiguLogin(required = false)
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId){
        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> map = trackInfoService.findUserTrackPaidList(trackId,userId);
        return Result.ok(map);
    }


    /**
     * 获取声音统计信息
     * /api/album/trackInfo/getTrackStatVo/{trackId}
     */
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId){
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }

    /**
     * 查询专辑声音分页列表
     * /api/album/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}
     */
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    @GuiguLogin(required = false)
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,
                                                             @PathVariable Long page,
                                                             @PathVariable Long limit){
        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        Page<AlbumTrackListVo> page1 = new Page<>(page,limit);
        Page<AlbumTrackListVo> albumTrackListVoPage = trackInfoService.findAlbumTrackPage(albumId,page1,userId);
        return Result.ok(albumTrackListVoPage);
    }

    /**
     * 上传声音
     * /api/album/trackInfo/uploadTrack
     * @param file  这个是上传的声音
     * @return 声音媒体唯一标识和声音媒体在线地址
     */
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String,String>> uploadTrack(MultipartFile file){
        Map<String,String> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    /**
     * 保存声音
     * /api/album/trackInfo/saveTrackInfo
     */
    @PostMapping("/trackInfo/saveTrackInfo")
    @GuiguLogin
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
        //从本地线程获取用户id
        Long userId = AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(trackInfoVo,userId);
        return Result.ok();
    }

    /**
     * 获取当前用户声音分页列表
     * /api/album/trackInfo/findUserTrackPage/{page}/{limit}
     */
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    @GuiguLogin
    public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable long page,
                                                        @PathVariable long limit,
                                                        @RequestBody TrackInfoQuery trackInfoQuery){
        Page<TrackListVo> page1 = new Page<>(page,limit);
        //从本地线程获取用户id
        Long userId = AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        page1=trackInfoService.findUserTrackPage(page1,trackInfoQuery);
        return Result.ok(page1);
    }

    /**
     * 查询声音信息
     * /api/album/trackInfo/getTrackInfo/{id}
     */
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable long id){
        TrackInfo byId = trackInfoService.getById(id);
        return Result.ok(byId);
    }


    /**
     *修改声音信息
     * api/album/trackInfo/updateTrackInfo/{id}
     */
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id,
                                  @RequestBody TrackInfoVo trackInfoVo){
        trackInfoService.updateTrackInfo(id,trackInfoVo);
        return Result.ok();
    }

    /**
     * 删除声音信息
     * /api/album/trackInfo/removeTrackInfo/{id}
     */
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable long id){
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }

    /**
     * 声音专辑统计
     * @param trackStatMqVo
     */
    public void updateTrackStat(TrackStatMqVo trackStatMqVo) {
        trackInfoService.updateTrackStat(trackStatMqVo);
    }
}

